//
//  ToolCoreData.m
//  Cat
//
//  Created by dllo on 15/12/2.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "ToolCoreData.h"
#import "CoreData.h"
#import "Picture.h"

@implementation ToolCoreData
{
    NSString *_urlStr;
}
- (BOOL)isHavePicture:(NSString *)url {
    
    //
    self.manager = [CoreData defaults];
    
    NSLog(@"%@", [self.manager applicationDocumentsDirectory]);
    
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Picture" inManagedObjectContext:self.manager.managedObjectContext];
    [fetchRequest setEntity:entity];
    // Specify criteria for filtering which objects to fetch
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@", @"url", @"%@"], url];
    [fetchRequest setPredicate:predicate];
    // Specify how the fetched objects should be sorted
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"url"
                                                                   ascending:YES];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error = nil;
    NSArray *fetchedObjects = [self.manager.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    
    _urlStr = url;
    for (Picture *model in fetchedObjects) {
        if (model.url != nil) {
            return NO;
        }
    }
    return YES;
}

- (void)savePicture {
    
    self.manager = [CoreData defaults];
    
    // 实体描述类
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Picture" inManagedObjectContext:self.manager.managedObjectContext];
    
    Picture *pic = [[Picture alloc] initWithEntity:entity insertIntoManagedObjectContext:self.manager.managedObjectContext];
    
    pic.url = _urlStr;
    
    [self.manager saveContext];
}

- (void)deletePicture {
    
    self.manager = [CoreData defaults];
    
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Picture" inManagedObjectContext:self.manager.managedObjectContext];
    [fetchRequest setEntity:entity];
    // Specify criteria for filtering which objects to fetch
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@", @"url", @"%@"], _urlStr];
    [fetchRequest setPredicate:predicate];
    // Specify how the fetched objects should be sorted
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"url"
                                                                   ascending:YES];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error = nil;
    NSArray *fetchedObjects = [self.manager.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    
    for (Picture *pic in fetchedObjects) {
        [self.manager.managedObjectContext deleteObject:pic];
    }
    
    [self.manager saveContext];
}

- (NSMutableArray *)takePicture {
    
    self.manager = [CoreData defaults];
    NSLog(@"%@", [self.manager applicationDocumentsDirectory]);
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Picture" inManagedObjectContext:self.manager.managedObjectContext];
    [fetchRequest setEntity:entity];
    
    NSPredicate *predicate = [NSPredicate predicateWithValue:YES];
    fetchRequest.predicate = predicate;

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"url"
                                                                   ascending:YES];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error = nil;
    NSArray *fetchedObjects = [self.manager.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    
    NSMutableArray *arr = [NSMutableArray array];
    for (Picture *modle in fetchedObjects) {
        [arr addObject:modle.url];
    }

    return arr;
}

+ (instancetype)defaults {
    static ToolCoreData *toolCoreData;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        toolCoreData = [[ToolCoreData alloc] init];
    });
    return toolCoreData;
}

- (void)deletePicture:(NSString *)url {
    self.manager = [CoreData defaults];
    
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Picture" inManagedObjectContext:self.manager.managedObjectContext];
    [fetchRequest setEntity:entity];
    // Specify criteria for filtering which objects to fetch
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@", @"url", @"%@"], url];
    [fetchRequest setPredicate:predicate];
    // Specify how the fetched objects should be sorted
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"url"
                                                                   ascending:YES];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error = nil;
    NSArray *fetchedObjects = [self.manager.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    
    for (Picture *pic in fetchedObjects) {
        [self.manager.managedObjectContext deleteObject:pic];
    }
    
    [self.manager saveContext];
}

@end
